package com.itlong.cloud.intercept;

import com.itlong.cloud.controller.SmartCardDemoController;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <desc>
 *     一卡通拦截器
 * </desc>
 *
 * @createDate 2018/06/12
 */
@Aspect
@Component
public class LoginIntercept {

    private static Logger LOG = LoggerFactory.getLogger(SmartCardDemoController.class);

 // 定义一个切入点
    @Pointcut("execution(* com.itlong.cloud.controller..*.*(..))")
    public void excudeService(){}

    @Around("excudeService()")
    public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //String[] interceptMethod= StringHandlerUtil.splitString(dontIntercept);
        String uri = request.getRequestURI();
        System.out.println (String.format("【%s】请求路由:%s，客户端地址：%s", DateUtil.fomatDate1(new Date()),
                uri, HttpProtocolUtil.getIpAddr(request)));
        // JSON.toJSONString(request.getParameterMap()
        String projectId = request.getParameter("projectId");

        MethodSignature signature = (MethodSignature) thisJoinPoint.getSignature();
        try {
            Object result =  thisJoinPoint.proceed();
            return result;
        } catch (Throwable e) {
            e.printStackTrace ();
        }
        return null;
    }
}
